<?php defined('SYSPATH') or die('No direct script access.');

class Model_Shop extends ORM
{  
    protected $_reload_on_wakeup = false;
    protected static $_current = null;             // current
    
    protected static function _compare($a, $b)
    {
        return $a['count'] < $b['count'];
    }
    
    public function get_shops(Model_Category $category, $brand = null, $color = null, $composition = null, $season = null, $price = null)
    {
        $stat = array();
        $ids = array(); 
        if ($category->childs) {
            $ids = explode(',', $category->childs);    
        }
        $ids[] = $category->id;
        
        $brand_id = $brand;
        if (is_array($brand)) {
            $brand_id = $brand['id'];   
        }
        
        $color_id = $color;
        if (is_array($color)) {
            $color_id = $color['id'];   
        }
        
        $composition_id = $composition;
        if (is_array($composition)) {
            $composition_id = $composition['id'];    
        }
        
        $season_id = $season;
        if (is_array($season)) {
            $season_id = $season['id'];    
        }
        
        $from_price = $to_price = null;
        if (isset($price['from']) && $price['from']) {
            $from_price = $price['from'];    
        }
        if (isset($price['to']) && $price['to']) {
            $to_price = $price['to'];    
        }
        
        if ($shops = Cache::instance()->get('category_shops_'.$category->id.'_'.$brand_id.'_'.$color_id.'_'.$composition_id.'_'.$season_id.'_'.$from_price.'_'.$to_price)) {
            return $shops;
        }
        
        $tmp_stat = DB::select('shop_id', array(DB::expr('COUNT( * )'), 'count'))
                    ->from('products')
                    ->where('category_id', 'IN', $ids)
                    ->where('available', '=', 1);
                    
        if ($brand_id) {
            $tmp_stat->where('brand_id', '=', $brand_id); 
        }
                    
        if ($color_id) {
            $tmp_stat->join('product_colors', 'LEFT')
                  ->on('product_colors.product_id', '=', 'products.id')
                  ->where('product_colors.color_id', '=', $color_id);    
        }
        
        if ($composition_id) {
            $tmp_stat->join('product_compositions', 'LEFT')
                  ->on('product_compositions.product_id', '=', 'products.id')
                  ->where('product_compositions.composition_id', '=', $composition_id);   
        }
        
        if ($season_id) {
            $tmp_stat->join('product_seasons', 'LEFT')
                  ->on('product_seasons.product_id', '=', 'products.id')
                  ->where('product_seasons.season_id', '=', $season_id);   
        }
        
        if ($from_price) {
            $tmp_stat->where('price', '>=', $from_price);    
        }
        if ($to_price) {
            $tmp_stat->where('price', '<=', $to_price); 
        }
                    
        $tmp_stat = $tmp_stat
                    ->group_by('shop_id')
                    ->order_by('count', 'DESC')
                    ->execute()
                    ->as_array();
        
        $ids = array();
        foreach ($tmp_stat as $item) {
            $ids[] = $item['shop_id'];
            $stat[$item['shop_id']] = $item['count'];    
        }
        if (!count($ids)) {
            return null;    
        }
        $shops = array();
        $tmp_shops = ORM::factory('shop')
                        ->where('id', 'IN', $ids)
                        ->find_all()
                        ->as_array();
        foreach ($tmp_shops as $shop) {
            $shop = $shop->as_array(); 
            $shops[$shop['id']] = $shop;
            $shops[$shop['id']]['count'] = $stat[$shop['id']];            
        } 
        usort($shops, array(__CLASS__, '_compare'));    
        
        Cache::instance()->set('category_shops_'.$category->id.'_'.$brand_id.'_'.$color_id.'_'.$composition_id.'_'.$season_id.'_'.$from_price.'_'.$to_price, $shops);
        return $shops;                                
    }
    
    public function get_shops_by_coupons($coupons)
    {
        $shops = array();
        $ids   = array();
        foreach ($coupons as $coupon) {
            $ids[$coupon->shop_id] = $coupon->shop_id;    
        }
        
        $shops = array();
        $tmp_shops = ORM::factory('shop')
                        ->where('id', 'IN', $ids)
                        ->find_all()
                        ->as_array();
        foreach ($tmp_shops as $shop) {
            $shops[$shop->id] = $shop;           
        } 
        
        return $shops;                                         
    }
    
    public function get_shop_by_url($url)
    {
        return ORM::factory('shop')->where('url', '=', $url)->find();    
    }
    
    public function get_top_shops()
    {
        if ($shops = Cache::instance()->get('top_shops')) {
            return $shops;
        }
        
        $date = new DateTime();        
        $shops = DB::select('*', array(DB::expr('(SELECT COUNT(*) FROM coupons WHERE shop_id = shops.id AND end_date > \''.$date->format('Y-m-d H:i:s').'\')'), 'count'))
                    ->from('shops')
                    ->order_by('count', 'DESC')
                    //->limit(30)
                    ->execute()
                    ->as_array();
        Cache::instance()->set('top_shops', $shops);             
        return $shops;   
    }
}